﻿using System;
using System.Linq;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using eeenno.ReaderEngine.Adapters;
using eeenno.ReaderEngine.Objects;
using eeenno.ReaderEngine.Utilities;

namespace eeenno.ReaderEngine.Helpers
{    
    /// <summary>
    /// Label utility class
    /// </summary>
    public class TagHelper
    {
        

        /// <summary>
        /// Builds the tag list
        /// </summary>
        /// <param name="tags">array of available tags</param>
        /// <param name="subscriptions">subscription information</param>
        /// <returns>Google Reader list of tags objects</returns>
        public List<Tag> BuildTagList(string[] tags, string[] subscriptions, string[] unread_count)
        {
            List<Tag> ret_val = new List<Tag>(tags.Length);
            
            for (int i = 0; i < tags.Length; i++)
            {
                if (!tags[i].Equals("starred") && !tags[i].Equals("broadcast") && !tags[i].Equals("blogger-following"))
                {
                    Tag tag = new Tag();                   
                    tag.name = tags[i];
                    tag.id = _TAG_ID_PREFIX + tag.name;
                    //prevents untagged reading list from appearing
                    if (tag.name != XmlEngine._NO_TAG_KEYWORD)
                    {
                        tag.subscription_list.subscriptions.Add(SubscriptionHelper.CreateReadingListSubscription(tag.name));
                    }

                    for (int j = 0; j < subscriptions.Length; j++)
                    {
                        string[] temp = subscriptions[j].Split(',');
                        if (temp[2].Equals(tags[i]))
                        {
                            Subscription sub = new Subscription();
                            sub.url = temp[0];
                            sub.title = temp[1];
                            sub.tag = temp[2];
                            sub.unread_count = 0;
                            sub.id = sub.url;
                            for (int k = 0; k < unread_count.Length; k++)
                            {
                                temp = unread_count[k].Split(',');
                                if (temp[0].Equals(sub.url))
                                {
                                    sub.unread_count = int.Parse(temp[1]);
                                }
                            }
                            tag.subscription_list.subscriptions.Add(sub);
                        }
                    }
                    
                    ret_val.Add(tag);
                }                
            }

            return ret_val;
        }
    }
}
